let storage = bui.storage();
let language;
let translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            router.$("#history").click(function () {
                bui.back()
            })

            let uiLoading = bui.loading({
                appendTo:"#loadingHistory",
                width: 40,
                height: 40,
                opacity: 0.5,
                autoClose: false,
                text:"加载中。。。"
            });

            let user = storage.get("user", 0)
            if (user && user.userId) {
                // uiLoading.show()
                bui.list({
                    id: "#scrollHistoryList",
                    url: global.api.selectUserVisit,
                    pageSize: 10, // 当pageSize 小于返回的数据大小的时候,则认为是最后一页,接口返回的数据最好能返回空数组,而不是null
                    data: {
                        user: user.userId
                    },
                    //如果分页的字段名不一样,通过field重新定义
                    field: {
                        page: "page",
                        size: "limit",
                        data: "data"
                    },
                    template: function(data) {
                        let fontSize = '0.3rem'
                        let fontSize2 = '0.24rem'
                        if (language !== "chinese_simplified") {
                            fontSize = '0.22rem'
                            fontSize2 = '0.18rem'
                        }

                        var html = "";
                        data.forEach(function(el, index) {
                            html += '<li class="bui-btn">\n' +
                                '                            <h3 class="item-title" style="font-size: '+fontSize+'">'+el.title+'</h3>\n' +
                                '                            <p class="item-text" style="font-size: '+fontSize2+'">'+formatDate(el.dateTime)+'</p>\n' +
                                '                        </li>'
                        });
                        uiLoading.stop()

                        setTimeout(function () {
                            try {
                                translate.execute();
                            }catch (e) {
                                console.log("切换语言报错")
                            }
                        },1000)
                        return html;
                    }
                })
            }

            router.$("#clear").click(function () {
                if (user && user.userId) {
                    uiLoading.show()
                    // bui.btn("#clear").submit(function(loading) {
                        bui.ajax({
                            url: global.api.clearUserVisit,
                            data: {user: user.userId},
                            method: "POST"
                        }).then(function(res){
                            if (res.code == '200') {
                                bui.alert('清除成功')
                            } else {
                                bui.alert('清除失败')
                            }
                            uiLoading.stop()
                            // loading.stop()
                        })
                    // })
                } else {
                    storage.set("dlTz", '3')
                    router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                }
            })
        }
    }
    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})

function formatDate(time) {
    let date = new Date(time * 1000);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    let D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
    return Y+M+D+h+m+s
}
